/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function Initialize(){
	// add search type action
	$("#searchtype").change(
		function () {
			var str = $("#searchtype option:selected").val();
                        var platid = $("#searchtype option:selected").attr("platid");
			$("#platid").val( platid );
			//$("#page").val(str);
			$( "#searchform" ).attr( "action", str );

			//change category
			if( str == "/shop_search_result" ){
				$( "#selcategory_product" ).hide();
				$( "#selcategory_product" ).attr('disabled', 'disabled');
				$( "#selcategory_shop" ).removeAttr('disabled');;
				$( "#selcategory_shop" ).show();
			}
			else{
				$( "#selcategory_shop" ).hide();
				$( "#selcategory_shop" ).attr('disabled', 'disabled');
				$( "#selcategory_product" ).removeAttr('disabled');;
				$( "#selcategory_product" ).show();
			}
		});

	$( "#searchform" ).submit(
		function(){
			CheckSearchAction();
		});

	var str = $('#keyword').val();
	if( "" == str ){
		$("#keyword").val("输入店铺名、掌柜名进行搜索...");
	}
	else{
		$("#keyword").val( str );
	}
}

function SetSelected( id, selectedValue ){
	$('#'+id).val(selectedValue);
}

function SetCheckBoxList( id, selectValues ){
	//	alert( selectValues );
	var idArray = selectValues.split(',');
	for(i in idArray){
		$( '#'+id+idArray[i] ).attr('checked', true);
	}
}

function SetFocus(){
	//	var str = $("#keyword").val();
	//when search input box set to focus, clear the field
	$("#keyword").focus(
		function(){
			if( $("#keyword").val() == "输入店铺名、掌柜名进行搜索..." ){
				$("#keyword").val("");
			}
		}
	)
	$("#keyword").blur(
		function(){
			if( $("#keyword").val() == "" ){
				$("#keyword").val("输入店铺名、掌柜名进行搜索...");
			}
		}
	)
}

function CheckSearchAction(){
	if( $("#keyword").val() == "输入店铺名、掌柜名进行搜索..." ){
		$("#keyword").val("");
	}
}

function SetAllSelectOptions(){
	//common header part
	SetSelected( "selcategory_product", $("#category").val() );
	//sub page part
	SetSelected( "selsortby", $("#sortby").val() );
	SetSelected( "sellocation", $("#location").val() );
	SetSelected( "selplatfilter", $("#plat_id").val() );
}

function SetAllCheckBox(){
	SetCheckBoxList( "feature", $("#feature_list").val() );
}

function LoadSortbyClass(){
	var sortby = $( "#sortby" ).val();
	//price
	if( sortby.indexOf( "price" ) >= 0 ){
		$( "#sortprice" ).addClass( "sort"+sortby );
	}
	else{
		$( "#sortprice" ).addClass( "sortprice" );
	}

	//service
	if( sortby.indexOf( "service" ) >= 0 ){
		$( "#sortservice" ).addClass( "sort"+sortby );
	}
	else{
		$( "#sortservice" ).addClass( "sortservice" );
	}

	//reality
	if( sortby.indexOf( "reality" ) >= 0 ){
		$( "#sortreality" ).addClass( "sort"+sortby );
	}
	else{
		$( "#sortreality" ).addClass( "sortreality" );
	}

	//velocity
	if( sortby.indexOf( "velocity" ) >= 0 ){
		$( "#sortvelocity" ).addClass( "sort"+sortby );
	}
	else{
		$( "#sortvelocity" ).addClass( "sortvelocity" );
	}
}

function SetSortby( type ){
	//reset the sort by selector
	var sortby = $( "#sortby" ).val();
	if( sortby == type+"-asc" ){
		$( "#sortby" ).val( type+"-desc" );
	}
	else if( sortby == type+"-desc" ){
		$( "#sortby" ).val( type+"-asc" );
	}
	else{
		$( "#sortby" ).val( type+"-asc" );
	}
	$( "#filterform" ).submit();
}

function InitTabClick(){
	if( $("#plat_id").val() >= 0 && $("#plat_id").val() < 100 ){
		$("#c2ctab").addClass( "filter_current" );
	}
	else{
		$("#b2ctab").addClass( "filter_current" );
	}

	$("#c2ctab").click(
		function(){
			//tab css style
			$("#c2ctab").addClass( "filter_current" );
			$("#b2ctab").removeClass( "filter_current" );

			// 0 means c2c
			$("#plat_id").val( 0 );
			$( "#filterform" ).submit();
		}
	);

	$("#b2ctab").click(
		function(){
			//tab css style
			$("#b2ctab").addClass( "filter_current" );
			$("#c2ctab").removeClass( "filter_current" );

			// 100 means b2c
			$("#plat_id").val( 100 );
			$( "#filterform" ).submit();
		}
	);
}

function ProductCatFilter( id ){
        var currentUrl = window.location.toString();
	var result = currentUrl.replace(/&category=[0-9]*/g, "");
        result = result.replace(/#/g, "");
        window.location = result+"&category="+id;
}

function LocationFilter( id ){
        var currentUrl = window.location.toString();
	var result = currentUrl.replace(/&location=[0-9]*/g, "");
        result = result.replace(/#/g, "");
        window.location = result+"&location="+id;
}

$(document).ready(function(){
	Initialize();
	SetFocus();
	SetAllSelectOptions();
	SetAllCheckBox();
	LoadSortbyClass();
	InitTabClick();

	$('#selsortby').change(
		function(){
			var selected = $("#selsortby option:selected");
			//append the sortby and submit the form
			$( "#sortby" ).val( selected.val() );
			$( "#filterform" ).submit();
		}
	);

	$('#sellocation').change(
		function(){
			var selected = $("#sellocation option:selected");
			//append the localtion and submit the form
			$( "#location" ).val( selected.val() );
			$( "#filterform" ).submit();
		}
	);
	
	$('#selplatfilter').change(
		function(){
			var selected = $("#selplatfilter option:selected");
			//append the localtion and submit the form
			$( "#plat_id" ).val( selected.val() );
			$( "#filterform" ).submit();
		}
	);

	//sort related block
	// price sort
	$("#sortprice").click(
		function(){
			SetSortby( "price" )
		}
	);
	$("#sortservice").click(
		function(){
			SetSortby( "service" )
		}
	);
	$("#sortreality").click(
		function(){
			SetSortby( "reality" )
		}
	);
	$("#sortvelocity").click(
		function(){
			SetSortby( "velocity" )
		}
	);

	//show hide category logic
	$( "#showAllCategory" ).click(
		function(){
			$("#searchhint_category_less").hide();
			$("#searchhint_category_all").show();
		}
	);

	$( "#showLessCategory" ).click(
		function(){
			$("#searchhint_category_all").hide();
			$("#searchhint_category_less").show();
		}
	);

    //search option default is invisiable
    //$("#searchFilterContent").hide()
    $( "#searchFilterTitle" ).click(
        function(){
            if( $("#searchFilterContent").is(':visible') ){
                $("#searchFilterContent").hide();
            }
            else{
                $("#searchFilterContent").show();
            }
        }
    );
});


$(window).load(function() {
	
	$(".hot_shop img").each(function() {
	
	 if((typeof this.naturalWidth != "undefined" && this.naturalWidth == 0 ) || this.readyState == 'uninitialized' ){
					
			this.src = edp_static_domain_url+'image/defaultpicture80.gif';
	}});
	
	$(".item_list img").each(function() {

		if((typeof this.naturalWidth != "undefined" && this.naturalWidth == 0 ) || this.readyState == 'uninitialized' ){
	
			this.src = edp_static_domain_url+'image/nopicture80.gif';
	}});

});
